package com.zjsru.oneday202208;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/8/4
 *
 * 输入：nums = [4,3,10,9,8]
 * 输出：[10,9]
 * 解释：子序列 [10,9] 和 [10,8] 是最小的、满足元素之和大于其他各元素之和的子序列。但是 [10,9] 的元素之和最大。
 *
 */
public class MinSubsequence {
    /**
     * 按照两个list的合去比较大小
     * 运行速度慢
     */
    
    public List<Integer> minSubsequence(int[] nums) {
        int len = nums.length;
        Arrays.sort(nums);
        List<Integer> list = new ArrayList<>();
        List<Integer> ans = new ArrayList<>();
        for (int num: nums) {
            list.add(num);
        }
        for (int i = 0; i < len; i++) {
            list.remove(list.size() - 1);
            ans.add(nums[len - i - 1]);
            int sum = (int) list.stream().mapToInt((s) -> s).summaryStatistics().getSum();
            int ansSum = (int) ans.stream().mapToInt((s) -> s).summaryStatistics().getSum();
            if(sum < ansSum){
                break;
            }
        }
        return ans;
    }
    
    public List<Integer> minSubsequence2(int[] nums) {
        int len = nums.length - 1, sum = 0, cur = 0;
        Arrays.sort(nums);
        List<Integer> ans = new ArrayList<>();
        for (int num:nums) {
            sum += num;
        }
        while(sum >= cur){
            sum -= nums[len];
            cur += nums[len];
            ans.add(nums[len--]);
        }
        return ans;
    }
    
    public static void main(String[] args) {
        MinSubsequence minSubsequence = new MinSubsequence();
//        int[] nums = new int[]{4,3,10,9,8};
        int[] nums = new int[]{8,8};
        System.out.println(minSubsequence.minSubsequence2(nums));
    }
}
